<?php
/**
 * @author Andrew Willmott <andrew.willmott@omni-fi.net>
 * @copyright Copyright (c) 2011, Andrew Willmott
 * @license http://www.opensource.org/licenses/mit-license.php MIT Licence
 * @version 1.0
 *
 * This page includes all the functions to allow a user to administer their account
 */
require ('functions.inc.php');

if (array_key_exists('action', $_GET)){
	//The user has specified an action
	if ($_GET['action'] == "addnew"){
		//The user wants to create a new account
		createAccount();
	}
}
else {
	listAccounts(1);
}


/**
 * This function lists all the accounts that are linked to a user.  
 */
function listAccounts($userID){
	$dbh = dbReadConnection();
	$stmt = $dbh->prepare("SELECT bank_accounts.bank_account_id, bank_accounts.account_address, bank_accounts.balance 
			FROM bank_accounts, mapping_user_bank
			WHERE (bank_accounts.bank_account_id = mapping_user_bank.bank_account)
			AND   (mapping_user_bank.user_account = :userid);");
	$stmt->bindValue(":userid", $userID, PDO::PARAM_INT);
	$stmt->execute();
	
	htmlHeader("Account");
	echo "</head>\n<body>\n\t<h1>Your accounts</h1>\n";
	echo "\t<table border='1'>\n\t\t<tr><th>Address</th><th>Balance</th></tr>\n";
	
	//Loop through the accounts and print them out
	while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		$address = $row['account_address'];
		$balance = $row['balance'];
		
		echo "\t\t<tr><td>$address</td><td>$balance</td></tr>\n";
	}
	
	echo "\t</table>\n";
	echo "\t<p><a href=account.php?action=addnew>Add New</a></p>\n";
	echo "</body>\n</html>";
}

/**
 * This function takes an account ID and displays its balance
 */
function getBalance($account){
	
}

function createAccount(){
	//Send JSON request to bitcoind to create account
	$bitcoin = getBitcoinBackendConnection();
	
	//Get ID for new account
	$dbh = dbWriteConnection();
	$dbh->beginTransaction();
	$stmt = $dbh->prepare("SELECT last_value FROM bank_accounts_bank_account_id_seq");
	$stmt->execute() or die('unable to select sequence number');
	$result = $stmt->fetch(PDO::FETCH_ASSOC);
	$sequence = $result['last_value'] + 1;
	
	//Create the Bitcoin account and return the address
	$bitcoinAddress = $bitcoin->getnewaddress("$sequence");
	
	//Insert the address into the bank account
	$stmt = $dbh->prepare("INSERT INTO bank_accounts (account_address) VALUES (:address);");
	$stmt->bindValue(":address", $bitcoinAddress, PDO::PARAM_STR);
	$stmt->execute() or die('unable to insert address into new bank account');
	
	//Map the bank account to the user account
	$stmt = $dbh->prepare("INSERT INTO mapping_user_bank(user_account, bank_account) VALUES (:useraccount, :bankaccount)");
	///ToDo: Use the logged in user's ID
	$stmt->bindValue(":useraccount", 1, PDO::PARAM_INT);
	$stmt->bindValue(":bankaccount", $sequence, PDO::PARAM_INT);
	$stmt->execute() or die('Unable to map account');
	
	///ToDo: Broadcast this association to the banking network
	
	//Commit the transaction
	$dbh->commit();
	
	htmlHeader("Account");
	echo "</head>\n<body>\n\t<h1>Bitcoin Account Created</h1>\n";
	echo "\t<p>Your Bitcoin account with address $bitcoinAddress has been created</p>\n";
	echo "</body>\n</html>";
}


?>